// 03admin/14feedback/feedback.js

var app = getApp()
var co = require('../../resource/js/common.js')
var _this, _data, _pageNoArray = [1, 1, 1, 1, 1],
   _feedBackListArray = [
      [],
      [],
      [],
      [],
      []
   ]
var _map = [0, 1, 2, 3, ''],
   _user
Page({
   data: {
      isShowInputLabelArr: [false, false],
      btnState: 1,
      solve_label: ['新任务', '未处理', '处理中', '已处理', '我的'],
      cateList: null,
      toastIsHide: true,
      feedbackList: [],
      keyword: "",
      projectkey: "",
      w_height: 800
   },
   onLoad: function(opt) {
      _this = this;
      _data = this.data
      _data.projectkey = opt.Project_Key
      wx.setNavigationBarTitle({
         title: opt.Project_Name + '-意见管理'
      })
      this.setData({
         w_height: wx.getSystemInfoSync().windowHeight - 70
      })
      _user = app.Data.userInfo
      _this.getFeedbackList(1)
   },
   onReachBottom: function() {
      if (_data.btnState != 4) {
         _this.getFeedbackList(_data.btnState)
      }
   },

   applyFirstDo: function() {
      console.log('【申请优先处理】')
   },

   agreeFirstDo: function() {
      console.log('【同意优先处理】')
   },
   //我来处理事件
   letMeDo: function(e) {
      var e_id = e.currentTarget.id
      var obj = _data.feedbackList[e_id]
      var param = {
         Worker_User_ID: _user.User_ID,
         Feedback_ID: obj.Feedback_ID,
         Status: 2
      }
      _this.saveApi(param, e_id)
   },
   //处理完成事件
   done: function(e) {
      var e_id = e.currentTarget.id
      var obj = _data.feedbackList[e_id]
      var param = {
         Worker_User_ID: _user.User_ID,
         Feedback_ID: obj.Feedback_ID,
         Status: 3
      }
      _this.saveApi(param, e_id)
   },
   //再反馈事件
   reFeedback: function(e) {
      console.log('【测试了还有问题】')
      var obj = _data.feedbackList[e.currentTarget.id]
      var Feedback_ID = obj.Feedback_ID
      wx.navigateTo({
         url: "/03admin/14feedback/01addFeedback/addFeedback?Feedback_ID=" + Feedback_ID,
      })
   },

   clickBtnState(e) {
      var e_id = e.currentTarget.id
      if (e_id == _data.btnState)
         return

      _this.setData({
         btnState: e_id
      })
      // _feedBackListArray[_data.btnState] = _data.feedbackList
      if (_feedBackListArray[e_id].length > 0) {
         this.setData({
            feedbackList: _feedBackListArray[e_id]
         })
      } else {
         this.getFeedbackList(e_id)
      }
   },
   //搜索事件
   search: function() {
      _pageNoArray[_data.btnState] = 1;
      _feedBackListArray[_data.btnState] = [];
      _this.getFeedbackList(_data.btnState)
   },
   //准备获取意见列表
   getFeedbackList: function(e_id) {
      var param = {
         page: _pageNoArray[e_id]
      }
      if (e_id == 4) { //我的按钮
         param.Worker_User_ID = _user.User_ID
         var url = 'meritList'
      } else {
         var url = 'getList'
         param.Status = _map[e_id]
         param.Project_Key = _data.projectkey
      }
      if (_data.keyword != '')
         param.keyword = _data.keyword
      this.getFeedbackListData(url, param, e_id)
   },
   //获取意见列表
   getFeedbackListData(url, param, e_id) {
      co.getShareApi('feedback/Femg/' + url, param, function(res) {
         if (res.code == 0) {
            _feedBackListArray[e_id] = _feedBackListArray[e_id].concat(res.data)
            _pageNoArray[_data.btnState]++
         } else if (res.code == 29300) {
            co.toastAllText('没有记录了')
         } else {
            co.toastAllText('请求数据错误')
         }
         _this.setData({
            feedbackList: _feedBackListArray[e_id]
         })
      })
   },

   showInputLabel: function(e) {
      if (e.detail.value.length > 1)
         return;
      var index = Number(e.currentTarget.id);
      var isShowArray = _data.isShowInputLabelArr;
      isShowArray[index] = true;
      _this.setData({
         isShowInputLabelArr: isShowArray
      })
   },
   // 获取滚动条当前位置
   onPageScroll: function(e) {
      _this.setData({
         floorstatus: e.scrollTop > 100
      });
   },
   //回到顶部
   goTop: function(e) { // 一键回到顶部
      if (wx.pageScrollTo) {
         wx.pageScrollTo({
            scrollTop: 0
         })
      } else {
         wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
         })
      }
   },
   //置顶事件
   setTop: function(e) {
      var obj = _data.feedbackList[e.currentTarget.id]
      var param = {
         Feedback_ID: obj.Feedback_ID,
         page: _pageNoArray[_data.btnState] - 1
      }
      co.getShareApi('feedback/Femg/setTop', param, function(res) {
         if (res.code == 0) {
            _feedBackListArray[_data.btnState] = res.data
            _this.setData({
               feedbackList: _feedBackListArray[_data.btnState]
            })
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
   //审核、安排处理 和不处理意见
   doFeedback: function(e) {
      var e_c = e.currentTarget
      var obj = _data.feedbackList[e_c.id]
      var param = {
         Feedback_ID: obj.Feedback_ID,
         Status: e_c.dataset.state,
         Auditor_User_ID: _user.User_ID
      }
      _this.saveApi(param, e_c.id, e_c.dataset.state)
   },
   //封装方法
   saveApi: function(param, e_id, state) {
      co.getShareApi('feedback/Femg/save', param, function(res) {
         if (res.code == 0) {
            wx.showToast({
               title: '成功',
               icon: 'success',
               duration: 2000
            })
            var list = _data.feedbackList
            if (state > 0 && state < 4) {
               console.log('list[e_id]==', list[e_id])
               _feedBackListArray[state] = [list[e_id]].concat(_feedBackListArray[state])
               console.log('feedBackListArray[state]==', _feedBackListArray[state])
            }
            list.splice(e_id, 1)
            _feedBackListArray[_data.btnState] = list
            _this.setData({
               feedbackList: list
            })


         } else {
            co.Modal(res.msg)
         }
      })
   },
   //监听输入框
   bindTextAreaBlur: function(e) {
      _data.keyword = e.detail.value
      console.log('keyword=====', _data.keyword)
   },
   //刷新事件
   Refresh: function(un_re_get) {
      _pageNoArray = [1, 1, 1, 1]
      _feedBackListArray = [
         [],
         [],
         [],
         [],
         []
      ]
      _this.setData({
         feedbackList: []
      })
      if (co.empty(un_re_get))
         _this.getFeedbackList(_data.btnState)
   },
   onUnload: function() {
      this.Refresh(1)
   }
})